package com.example.rwq_test.niuke.stack;

import java.util.Stack;

/**
 * TODO note
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2023/12/22
 */
public class ArrayOccurHalfMore {
    public static void main(String[] args) {
        int[] arr = {2,1,2,3,2,4,2,2,2,5,6,7,2};
        int a = getNum1(arr);
        System.out.println(a);
    }

    private static int getNum1(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        for (int i : arr) {
            if (stack.isEmpty() || stack.peek() == i) {
                stack.push(i);
            } else {
                stack.pop();
            }
        }
        return stack.pop();
    }

    private static int getNum(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < arr.length; i++) {
            if (stack.isEmpty()) {
                stack.push(arr[i]);
                continue;
            }
            if (arr[i] != stack.peek()) {
                stack.pop();
            } else {
                stack.push(arr[i]);
            }
        }
        return stack.peek();
    }
}
